package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.InviteCode;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface InviteCodeMapper {
    @Delete({
        "delete from invite_code",
        "where invite_code_id = #{inviteCodeId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer inviteCodeId);

    @Insert({
        "insert into invite_code ( class_id, ",
        "creator_id, code, ",
        "receiver_id, is_used)",
        "values (#{classId,jdbcType=INTEGER}, ",
        "#{creatorId,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR}, ",
        "#{receiverId,jdbcType=INTEGER}, #{isUsed,jdbcType=BIT})"
    })
    int insert(InviteCode record);

    @Select({
        "select",
        "invite_code_id, class_id, creator_id, code, receiver_id, is_used",
        "from invite_code",
        "where invite_code_id = #{inviteCodeId,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="invite_code_id", property="inviteCodeId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
        @Result(column="creator_id", property="creatorId", jdbcType=JdbcType.INTEGER),
        @Result(column="code", property="code", jdbcType=JdbcType.VARCHAR),
        @Result(column="receiver_id", property="receiverId", jdbcType=JdbcType.INTEGER),
        @Result(column="is_used", property="isUsed", jdbcType=JdbcType.BIT)
    })
    InviteCode selectByPrimaryKey(Integer inviteCodeId);

    @Select({
        "select",
        "invite_code_id, class_id, creator_id, code, receiver_id, is_used",
        "from invite_code"
    })
    @ResultMap("ResultMap")
    List<InviteCode> selectAll();

    //根据classId查询InviteCode
    @SelectProvider(type = QueryBuilder.class,method = "inviteCodeQueryByClassId")
    @ResultMap("ResultMap")
    List<InviteCode> inviteCodeQueryByClassId(@Param("classId")int classId);

    @Update({
        "update invite_code",
        "set class_id = #{classId,jdbcType=INTEGER},",
          "creator_id = #{creatorId,jdbcType=INTEGER},",
          "code = #{code,jdbcType=VARCHAR},",
          "receiver_id = #{receiverId,jdbcType=INTEGER},",
          "is_used = #{isUsed,jdbcType=BIT}",
        "where invite_code_id = #{inviteCodeId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(InviteCode record);
}